#include <iostream>

#include "app.hpp"
#include "common.hpp"
#include "hik_camera.hpp"
#include "nn_detector.hpp"
#include "timer.hpp"

class MaskNNDetector : private App {
 private:
  HikCamera cam_;

  TRTDetector detector_;
  std::vector<TRT::Detection> dets_;
  component::Recorder recorder_ = component::Recorder("DetectionThread");

 public:
  MaskNNDetector(const std::string log_path) : App(log_path) {
    SPDLOG_WARN("***** Setting Up System. *****");
    cam_.Open(0);
    cam_.Setup(640, 480);

    detector_.SetEnginePath("../../../../data/nn/yolo_mine.engine");
    detector_.Init();
  }

  ~MaskNNDetector() { SPDLOG_WARN("***** Shuted Down System. *****"); }

  void Run() {
    SPDLOG_WARN("******* Running. ********");
    cv::Mat frame;

    while (1) {
      dets_.clear();
      cam_.GetFrame(frame);
      if (frame.empty()) continue;
      dets_ = detector_.Infer(frame);

      if (dets_.size() > 0)
        for (auto it = dets_.begin(); it != dets_.end(); ++it) {
          component::Draw(frame, *it);
        }

      cv::imshow("show", frame);
      if (' ' == cv::waitKey(10)) {
        cv::waitKey(0);
      }

      recorder_.Record();
    }
  }
};

int main(int argc, char const* argv[]) {
  (void)argc;
  (void)argv;

  MaskNNDetector mask("logs/mask.log");
  mask.Run();

  return EXIT_SUCCESS;
}
